package com.ruoyi.project.user.userprivilege.controller;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.userprivilege.domain.UserPrivilege;
import com.ruoyi.project.user.userprivilege.service.IUserPrivilegeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 用户特权Controller
 *
 * @author shixiangwen
 * @date 2022-10-11
 */
@Controller
@RequestMapping("/user/userprivilege")
public class UserPrivilegeController extends BaseController {
    private String prefix = "user/userprivilege";

    @Autowired
    private IUserPrivilegeService userPrivilegeService;

    @RequiresPermissions("user:userprivilege:view")
    @GetMapping("/{kkid}/{appId}")
    public String userprivilege(@PathVariable("kkid") Long kkid, @PathVariable("appId") String appId, ModelMap mmap) {
        mmap.put("kkid", kkid);
        mmap.put("appId", appId);
        return prefix + "/userprivilege";
    }

    /**
     * 查询用户特权列表
     */
    @RequiresPermissions("user:userprivilege:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserPrivilege userPrivilege) {
        startPage();
        List<UserPrivilege> list = userPrivilegeService.selectUserPrivilegeList(userPrivilege);
        return getDataTable(list);
    }

    /**
     * 导出用户特权列表
     */
    @RequiresPermissions("user:userprivilege:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserPrivilege userPrivilege) {
        List<UserPrivilege> list = userPrivilegeService.selectUserPrivilegeList(userPrivilege);
        ExcelUtil<UserPrivilege> util = new ExcelUtil<UserPrivilege>(UserPrivilege.class);
        return util.exportExcel(list, "userprivilege");
    }

    /**
     * 新增用户特权
     */
    @GetMapping("/add/{kkno}/{kkid}/{appId}")
    public String add(@PathVariable("kkno") String kkno, @PathVariable("kkid") Long kkid, @PathVariable("appId") String appId, ModelMap mmap) {
        mmap.put("kkno", kkno);
        mmap.put("appId", appId);
        mmap.put("kkid", kkid);
        if (StringUtils.equals("kkdictios", appId)) {
            mmap.put("privilegeType", "18");
        } else if (StringUtils.equals("kkdict", appId)) {
            mmap.put("privilegeType", "18");
        } else if (StringUtils.equals("dictkx", appId) || StringUtils.equals("dictkxios", appId)) {
            mmap.put("privilegeType", "9");
        } else if (StringUtils.equals("dictsw", appId) || StringUtils.equals("dictswios", appId)) {
            mmap.put("privilegeType", "11");
        } else if (StringUtils.equals("cidianhy", appId)) {
            mmap.put("privilegeType", "16");
        } else if (StringUtils.equals("kkpoem", appId)) {
            mmap.put("privilegeType", "20");
        } else if (StringUtils.equals("wyw", appId)) {
            mmap.put("privilegeType", "21");
        } else if (StringUtils.equals("kkyuwen", appId) || StringUtils.equals("kkyuwen100", appId)) {
            mmap.put("privilegeType", "5");
        }
        return prefix + "/add";
    }

    /**
     * 新增保存用户特权
     */
    @RequiresPermissions("user:userprivilege:add")
    @Log(title = "发放会员", businessType = BusinessType.GRANT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserPrivilege userPrivilege) {
        UserPrivilege query = new UserPrivilege();
        query.setAppId(userPrivilege.getAppId());
        query.setKkid(userPrivilege.getKkid());
        query.setType(userPrivilege.getType());
        UserPrivilege up = userPrivilegeService.selectUserPrivilege(query);
        if (up == null) {
            userPrivilege.setCreateTime(DateUtils.getNowDate());
            userPrivilege.setExpireTime(DateUtils.addMonths(DateUtils.getNowDate(), userPrivilege.getPeriod()));
            return toAjax(userPrivilegeService.insertUserPrivilege(userPrivilege));
        } else {
            Date expireTime;
            if (up.getExpireTime().after(DateUtils.getNowDate())) {
                expireTime = DateUtils.addMonths(up.getExpireTime(), userPrivilege.getPeriod());
            } else {
                expireTime = DateUtils.addMonths(DateUtils.getNowDate(), userPrivilege.getPeriod());
            }
            up.setExpireTime(expireTime);
            return toAjax(userPrivilegeService.updateUserPrivilege(up));
        }
    }

    /**
     * 修改用户特权
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        UserPrivilege userPrivilege = userPrivilegeService.selectUserPrivilegeById(id);
        mmap.put("userPrivilege", userPrivilege);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户特权
     */
    @RequiresPermissions("user:userprivilege:edit")
    @Log(title = "用户特权", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserPrivilege userPrivilege) {
        return toAjax(userPrivilegeService.updateUserPrivilege(userPrivilege));
    }

    /**
     * 删除用户特权
     */
    @RequiresPermissions("user:userprivilege:remove")
    @Log(title = "用户特权", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userPrivilegeService.deleteUserPrivilegeByIds(ids));
    }
}
